/*
 * LineStyles.java
 * 
 * Created by demory on Jan 29, 2010, 8:44:04 PM
 * 
 * Copyright 2008 David D. Emory
 * 
 * This file is part of Five Points. See <http://www.fpdev.org> for
 * additional information regarding the project.
 * 
 * Five Points is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * Five Points is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with Five Points.  If not, see <http://www.gnu.org/licenses/>.
 */

package org.fpdev.apps.cart.network;

import java.awt.Color;
import java.util.Collections;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import org.w3c.dom.NamedNodeMap;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;

/**
 *
 * @author demory
 */

public class LineStyles {

  private Map<Integer, LineStyle> styles_;

  private int lastID_;

  public LineStyles() {
    styles_ = new HashMap<Integer, LineStyle>();
    lastID_ = 0;
  }

  public void addStyle(LineStyle style) {
    int id = ++lastID_;
    styles_.put(id, style);
    style.setID(id);
  }

  public LineStyle getStyle(int id) {
    return styles_.get(id);
  }

  public List<LineStyle> getList() {
    List<LineStyle> list = new LinkedList<LineStyle>(styles_.values());
    Collections.sort(list);
    return list;
  }

  public void removeStyle(LineStyle style) {
    styles_.remove(style.getID());
  }

  public void readFromXML(Node cartNode) {
    NodeList topNodes = cartNode.getChildNodes();
    for (int i = 0; i < topNodes.getLength(); i++) {
      if(topNodes.item(i).getNodeName().equals("styles")) {
        NodeList stylesNodes = topNodes.item(i).getChildNodes();
        int maxID = 0;
        for (int is = 0; is < stylesNodes.getLength(); is++) {
          if(stylesNodes.item(is).getNodeName().equals("style")) {
            Node styleNode = stylesNodes.item(is);
            NamedNodeMap attributes = styleNode.getAttributes();
            int id = new Integer(attributes.getNamedItem("id").getNodeValue()).intValue();
            if(id > maxID) maxID = id;
            String name = attributes.getNamedItem("name").getNodeValue();

            System.out.println("found style id="+id);
            LineStyle style = new LineStyle(id, name);

            NodeList subsNodes = stylesNodes.item(is).getChildNodes();
            for (int iss = 0; iss < subsNodes.getLength(); iss++) {
              if(subsNodes.item(iss).getNodeName().equals("substyle")) {
                Node subNode = subsNodes.item(iss);
                attributes = subNode.getAttributes();
                Color color = new Color(new Integer(attributes.getNamedItem("color").getNodeValue()).intValue());
                int width = new Integer(attributes.getNamedItem("width").getNodeValue()).intValue();
                float dash[] = null;
                try {
                  String dashStr = attributes.getNamedItem("dash").getNodeValue();
                  dash = SubStyle.parseDash(dashStr);
                } catch(Exception ex) { }
                style.addStyle(new SubStyle(width, color, dash));
              }
            }
            styles_.put(id, style);
          }
        }
        lastID_ = maxID;
      }
    }
  }
  
  public String getXML(String indent) {
    String xml = "";
    xml += indent+"<styles>\n";
    for(LineStyle style : styles_.values()) {
      xml += indent+"  <style id=\""+style.getID()+"\" name=\""+style.getStyleName()+"\">\n";
      for(SubStyle sub : style.getSubStyles()) {
        xml += indent+"    <substyle color=\""+sub.getColor().getRGB()+"\" width=\""+sub.getWidth()+"\" dash=\""+sub.getDashStr()+"\" />\n";
      }
      xml += indent+"  </style>\n";
    }
    xml += indent+"</styles>\n";

    return xml;
  }
}
